package com.sixth.service.Impl;

import com.sixth.dao.EntryDao;
import com.sixth.dao.GoodsDao;
import com.sixth.dao.OutlibraryDao;
import com.sixth.entity.*;
import com.sixth.service.EntryService;
import com.sixth.util.CommonResult;
import com.sixth.util.LayTab;
import com.sixth.util.TableLayUi;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @program: project01
 * @description:
 * @author: 老魏
 * @create: 2021-12-18 10:43
 **/
@Service(value = "entryService ")
public class EntryServiceImpl implements EntryService {
    @Autowired
    private EntryDao entryDao;
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private OutlibraryDao outlibraryDao;
    @Autowired
    private GoodsDao goodsDao;

    public EntryServiceImpl() {
    }

    public LayTab getAllEntry(Warehousing warehousing, Limit limit) {
        Staff staff = (Staff)httpSession.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Integer roleid = staff.getRoleid();
        List<Warehousing> allEntry = entryDao.getAllEntry(warehousing, limit, branchid, roleid);
        LayTab LayTab = new LayTab(entryDao.getAllEntryCount(warehousing, branchid, roleid), allEntry);
        return LayTab;
    }

    public LayTab getAllShoping(Goods goods, Limit limit, Integer bId) {
        Staff staff = (Staff)httpSession.getAttribute("staff");
        Integer branchid1 = staff.getBranchid();
        List<Goods> allShoping = entryDao.getAllShoping(goods, limit, bId);
        LayTab TableLayUi = new LayTab(entryDao.getAllShopingCount(goods, bId), allShoping);
        return TableLayUi;
    }

    public CommonResult addAllEntry(String allEntry) {
        List<Warehousings> warehousings = JSONArray.parseArray(allEntry, Warehousings.class);
        Staff staff = (Staff)httpSession.getAttribute("staff");
        Warehousing inBound = new Warehousing();
        Integer branchid = inBound.getBranchid();
        inBound.setDate(new Date());
        inBound.setStaffid(staff.getStaffid() + "");
        inBound.setBranchid(staff.getBranchid());
        int i = entryDao.addAllEntry(inBound);
        Integer id = inBound.getId();
        boolean flag = false;
        int j = 0;
        if (i <= 0) {
            return CommonResult.fail("失败1");
        } else {
            Warehousings warehousing;
            for(Iterator var10 = warehousings.iterator(); var10.hasNext(); j = entryDao.addAllEntry2(id, warehousing.getGoodsid(), warehousing.getNum(), inBound.getBranchid())) {
                warehousing = (Warehousings)var10.next();
            }
            return j != 0 ? CommonResult.success() : CommonResult.fail("失败2");
        }
    }


    public CommonResult addAllEntry3(String allEntry) {
        List<Warehousings> warehousings = JSONArray.parseArray(allEntry, Warehousings.class);
        Staff staff = (Staff)httpSession.getAttribute("staff");
        Warehousing inBound = new Warehousing();
        inBound.setDate(new Date());
        inBound.setStaffid(staff.getStaffid() + "");
        inBound.setBranchid(staff.getBranchid());
        entryDao.addAllEntry3(inBound);
        boolean flag = false;
        Iterator var7 = warehousings.iterator();

        while(var7.hasNext()) {
            Warehousings warehousing = (Warehousings)var7.next();
            int j = entryDao.addAllEntry2(warehousing.getId(), warehousing.getGoodsid(), warehousing.getNum(), warehousing.getBranchid());
            if (j == 0) {
                flag = true;
            }
        }

        if (!flag) {
            return CommonResult.success();
        } else {
            return CommonResult.fail("失败2");
        }
    }

    public LayTab getAllWarehousings(Staff staff, Warehousings warehousings, Integer id) {
        List<Map> allWarehousings = entryDao.getAllWarehousings(staff, warehousings, id);
        LayTab LayTab = new LayTab(allWarehousings.size(), allWarehousings);
        return LayTab;
    }

    public CommonResult addGoods(String allGoods) {
        List<Goods> goods = JSONArray.parseArray(allGoods, Goods.class);
        Staff staff = (Staff)httpSession.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Iterator var5 = goods.iterator();

        while(var5.hasNext()) {
            Goods good = (Goods)var5.next();
            Integer goodsid = good.getGoodsid();
            BranchAndGoods branchAndGoods = outlibraryDao.getBranchAndGoods(goodsid, branchid);
            if (branchAndGoods != null) {
                return CommonResult.fail("该分店有该商品！");
            }

            goodsDao.addBranchGoods(branchid, goodsid);
        }

        return CommonResult.success();
    }
}
